//
//  AdvertisingDetailsViewController.m
//  HotWord
//
//  Created by Rainbow on 1/18/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AdvertisingDetailsViewController.h"
#import "HotWordAppDelegate.h"

@implementation AdvertisingDetailsViewController
@synthesize advertising;
@synthesize categoryLabel;
@synthesize timeLabel;
@synthesize titleLabel;
@synthesize contentTextView;
@synthesize linkTextView;

-(id)initWithAdvertising:(Advertising *)ad
{
    self = [super init];
    if (self) {
        self.advertising = ad;
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

-(void)updateToolBtn
{   
    HotWordAppDelegate *appDelegate= (HotWordAppDelegate *)[UIApplication sharedApplication].delegate;
    [[appDelegate.MainToolBar viewWithTag:77]removeFromSuperview];
    [[appDelegate.MainToolBar viewWithTag:66]removeFromSuperview];
    [[appDelegate.MainToolBar viewWithTag:88]removeFromSuperview];
    
    UIButton *backToMenuBtn=[[[UIButton alloc]init] autorelease];
    backToMenuBtn.frame=CGRectMake(-50, 20, 100, 100);
    backToMenuBtn.tag=66;
    [backToMenuBtn addTarget:self action:@selector(retrunBack) forControlEvents:UIControlEventTouchUpInside];
    [backToMenuBtn setImage:[UIImage imageNamed:@"back_arrow.png"] forState:UIControlStateNormal];
    [backToMenuBtn setImage:[UIImage imageNamed:@"back_arrow.png"] forState:UIControlStateHighlighted];
    [backToMenuBtn setBackgroundImage:[UIImage imageNamed:@"tap_light.png"] forState:UIControlStateHighlighted];
    backToMenuBtn.alpha=0.0f; 
    [appDelegate.MainToolBar addSubview:backToMenuBtn];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.2f];
    [UIView setAnimationDelay:0.2f];
    
    backToMenuBtn.alpha=1.0f; 
    [UIView commitAnimations];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.categoryLabel.text = self.advertising.category;
    self.timeLabel.text = [UtilHelper getUSDateByInterval:[self.advertising.createTime doubleValue]];
    self.titleLabel.text = self.advertising.title;
    self.contentTextView.text = self.advertising.details;
    self.linkTextView.text = self.advertising.adUrl;
    NSInteger titleHeight = [self.titleLabel.text sizeWithFont:self.titleLabel.font constrainedToSize:self.titleLabel.frame.size lineBreakMode:UILineBreakModeCharacterWrap].height;
    self.titleLabel.top = self.titleLabel.top + self.titleLabel.height - titleHeight;
    self.titleLabel.height = titleHeight;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self updateToolBtn];
}

- (void)viewDidUnload
{
    [self setCategoryLabel:nil];
    [self setTimeLabel:nil];
    [self setTitleLabel:nil];
    [self setContentTextView:nil];
    [self setLinkTextView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)retrunBack
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc
{
    [advertising release];
    [categoryLabel release];
    [timeLabel release];
    [titleLabel release];
    [contentTextView release];
    [linkTextView release];
    [super dealloc];
}

@end
